/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package boundary;

import entity.Event;
import entity.User;
import java.security.Principal;
import java.util.Date;
import java.util.List;
import java.util.Vector;
import javax.ejb.EJB;
import javax.ejb.Stateless;
import javax.inject.Inject;
import javax.inject.Named;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import security.EventBean;


/**
 *
 * @author MONESI
 */

@Stateless
@Named
public class EventManager {
    
    @PersistenceContext
    EntityManager em;
    
    @Inject
    Principal principal;
   
    
       
   
    
     public List<String> getUsersByEvent(int idEvent, String loggedUser){
         
        List<String> users = em.createQuery("SELECT e.userInvited FROM PARTICIPANT e WHERE e.eventId LIKE :idEvent AND e.email LIKE :logged").setParameter("idEvent", idEvent).setParameter("logged", loggedUser).getResultList();
        

        return users;
    }
    
    

    public void save(Event event) {
        int x;
        int n;
        List<Integer> i = em.createQuery("SELECT MAX(e.eventId) FROM EVENTS e ").getResultList();
        
        x=i.size()-1;
        n=i.get(x);
        n++;
        event.setid(n);
        
        em.persist(event);
        
    }
    
      public List<Date> giorni(String loggedUser){
        return em.createQuery("SELECT e.date FROM EVENTS e WHERE e.creatormail LIKE :logged GROUP BY e.date").setParameter("logged", loggedUser).getResultList();
    }
        
    
    public List<String> eventi(String loggedUser, Date data){
        
        return em.createQuery("SELECT e.name FROM EVENTS e WHERE e.creatormail LIKE :logged AND e.date LIKE :date").setParameter("logged", loggedUser).setParameter("date", data).getResultList();
    }
    
    
    public List<Event> creaEventi(String loggedUser){
        
        return em.createQuery("SELECT e FROM EVENTS e WHERE e.creatormail LIKE :logged").setParameter("logged", loggedUser).getResultList();
    }
    
     public List<String> getEventList(String loggedUser){
        List<String> events = em.createQuery("SELECT e.name FROM EVENTS e WHERE e.creatormail LIKE :logged").setParameter("logged", loggedUser).getResultList();
        return events;
    }
        
    public List<String> getEventByDate(Date date, String loggedUser){
         
        List<String> events = em.createQuery("SELECT e.name FROM EVENTS e WHERE e.date LIKE :data AND e.creatormail LIKE :logged").setParameter("data", date).setParameter("logged", loggedUser).getResultList();
        
        return events;
    }
    
    
    public User getLoggedUser() {
        return em.find(User.class, principal.getName());
    }
    
}

